const koa = require("koa");
let app = new koa();
const R = require("koa-router");
let router = new R();
const axios = require("axios");
const path = require("path");
// 引入静态资源管理中间件
const koaStatic = require("koa-static");
// 静态目录可以注册多个
const cors = require("koa2-cors");
// app.use(koaStatic('static'));
app.use(cors());
app.use(koaStatic(path.join(__dirname, "dist")));
router.get("/search", async (ctx) => {
  let keyword = ctx.query.key;
  let page = ctx.query.page;
  res = await lendMusicList(keyword, page);
  // console.log(res);
  ctx.body = res;
});
router.get("/lyric", async (ctx) => {
  let rid = ctx.query.id || 1;
  axios.defaults.headers.Cookie =
    "Hm_lvt_cdb524f42f0ce19b169a8071123a4797=1621298059; Hm_lpvt_cdb524f42f0ce19b169a8071123a4797=1621298059; _ga=GA1.2.1907350994.1621298059; _gid=GA1.2.154218489.1621298059; kw_token=JRNWQOZHJSO";
  axios.defaults.headers.csrf = "JRNWQOZHJSO";
  axios.defaults.headers.Referer =
    "http://www.kuwo.cn/search/list?key=%E6%9D%B0%E4%BC%A6";
  axios.defaults.headers["User-Agent"] =
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36";
  // console.log(rid);
  // let url = 'http://www.kuwo.cn/play_detail/9795527';
  let url = `http://m.kuwo.cn/newh5/singles/songinfoandlrc?musicId=${rid}&httpsStatus=1&reqId=9df48650-b873-11eb-a221-27604b91e02d`;
  let xxx = await axios.get(url);
  // console.log(xxx.data.data);
  ctx.body = xxx.data;
});

router.get("/musicurl", async (ctx) => {
  let rid = ctx.query.id || 1;
  let info = await lendMusicUrl(rid);
  if (info.status == 200) {
    ctx.body = {
      code: 1,
      data: info.data,
      msg: "OK",
    };
  } else {
    ctx.body = {
      code: 0,
      data: "",
      msg: "Error",
    };
  }
});

async function lendMusicList(keyword, page) {
  axios.defaults.headers.Cookie =
    "Hm_lvt_cdb524f42f0ce19b169a8071123a4797=1621298059; Hm_lpvt_cdb524f42f0ce19b169a8071123a4797=1621298059; _ga=GA1.2.1907350994.1621298059; _gid=GA1.2.154218489.1621298059; kw_token=JRNWQOZHJSO";
  // axios.defaults.headers.Cookie = '_ga=GA1.2.1739991127.1621329769; _gid=GA1.2.1493779855.1621329769; _gat=1; Hm_lvt_cdb524f42f0ce19b169a8071123a4797=1621329769,1621337704,1621385749,1621400291; Hm_lpvt_cdb524f42f0ce19b169a8071123a4797=1621400291; kw_token=EMWNCVJ2DE5';
  axios.defaults.headers.csrf = "JRNWQOZHJSO";
  axios.defaults.headers.Referer =
    "http://www.kuwo.cn/search/list?key=%E6%9D%B0%E4%BC%A6";
  axios.defaults.headers["User-Agent"] =
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36";

  keyword = encodeURIComponent(keyword);

  let url = `http://www.kuwo.cn/api/www/search/searchMusicBykeyWord?key=${keyword}&pn=${page}&rn=30&httpsStatus=1&reqId=7a892d91-b771-11eb-985c-ebeb1cde5017`;
  let xx = await axios.get(url);
  //   console.log(xx);
  return xx.data.data;
}

async function lendMusicUrl(rid) {
  axios.defaults.headers.Cookie =
    "Hm_lvt_cdb524f42f0ce19b169a8071123a4797=1621298059; Hm_lpvt_cdb524f42f0ce19b169a8071123a4797=1621298059; _ga=GA1.2.1907350994.1621298059; _gid=GA1.2.154218489.1621298059; kw_token=JRNWQOZHJSO";
  axios.defaults.headers.csrf = "JRNWQOZHJSO";
  axios.defaults.headers.Referer =
    "http://www.kuwo.cn/search/list?key=%E6%9D%B0%E4%BC%A6";
  axios.defaults.headers["User-Agent"] =
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36";
  let url = `http://www.kuwo.cn/url?format=mp3&rid=${rid}&response=url&type=convert_url3&br=128kmp3&from=web&t=1621302359167&httpsStatus=1&reqId=cb6c0801-b77a-11eb-a826-1d461a718d87`;
  return await axios.get(url);
}

router.get("/music", async (ctx) => {
  // http://www.kuwo.cn/api/www/music/musicInfo?mid=167848426&httpsStatus=1&reqId=45003260-b7b0-11eb-8f73-09ca0289590f

  axios.defaults.headers.Cookie =
    "Hm_lvt_cdb524f42f0ce19b169a8071123a4797=1621298059; Hm_lpvt_cdb524f42f0ce19b169a8071123a4797=1621298059; _ga=GA1.2.1907350994.1621298059; _gid=GA1.2.154218489.1621298059; kw_token=JRNWQOZHJSO";
  axios.defaults.headers.csrf = "JRNWQOZHJSO";
  axios.defaults.headers.Referer =
    "http://www.kuwo.cn/search/list?key=%E6%9D%B0%E4%BC%A6";
  axios.defaults.headers["User-Agent"] =
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36";

  let rid = ctx.query.id || 1;
  let url = `http://www.kuwo.cn/api/www/music/musicInfo?mid=${rid}&httpsStatus=1&reqId=45003260-b7b0-11eb-8f73-09ca0289590f`;
  let xx = await axios.get(url);
  ctx.body = xx.data.data;
});
app.use(router.routes()).use(router.allowedMethods());

app.listen(8080, () => {
  console.log("启动成功,浏览器访问localhost:8080开始听歌");
  console.log("可听VIP资源");
  console.log("此项目为简约移动端项目，浏览器f12，进入移动端调试，体验最佳");
  console.log("欢欢欢欢make");
});
